package com.edu.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;


public class Student implements ApplicationContextAware, BeanNameAware, InitializingBean, DisposableBean,Study {

    private int stuId;

    private String stuName;

    private int stuAge;

    private boolean graduated;

    public boolean isGraduated() {
        return graduated;
    }

    public void setGraduated(boolean graduated) {
        this.graduated = graduated;
    }

    public String getBeanName() {
        return beanName;
    }

    private String beanName;

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        System.out.println("02 Spring 调用了 对象的 setter方法");
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                ", graduated=" + graduated +
                ", beanName='" + beanName + '\'' +
                '}';
    }

    public Student() {
        System.out.println("01 Spring 容器调用了对象的构造方法");
    }

    @Override
    public void setBeanName(String s) {
        this.beanName = s;
        System.out.println("03 执行了 BeanNameAware接口的方法，注入了beanName");
    }

    @Override
    public void setApplicationContext(ApplicationContext app) throws BeansException {
        System.out.println("03 执行了 ApplicationContextAware接口的方法，注入了容器本身");
//        System.out.println(app);
    }

    public void myInit(){
        System.out.println("06 执行了自定义的 初始化方法");
//        if(this.stuAge < 0){
//            this.stuAge = 18;
//        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("05 执行了 InitializingBean的 初始化方法");
        this.stuAge = -200;
    }

    public void myDestroy(){
        this.graduated = true;
        System.out.println("10 执行了自定义的销毁方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("09 执行了DisposableBean 接口的销毁方法");
    }

    public void study(){
        System.out.println("08 执行了bean的 业务功能 学生正在学习");
    }
}
